﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using HGZP.PLL;

namespace HGZP.PLL
{
    public enum IsUnicodeColumn { Yes, No }//是否Unicode列类型
    /// <summary>
    /// 模块作者: 郑文亮
    /// 模块功能: 统一的输入检查(包括textbox[整数和字符] combox)
    /// 模块日期: 2009-2-10
    /// </summary>
    public class MyValidating
    {
        static public void MyTextBoxValidating(object sender, CancelEventArgs e, string columnDescription)
        {
            MyTextBoxValidating(sender, e, columnDescription, IsUnicodeColumn.No);
        }
        static public void MyTextBoxValidating(object sender, CancelEventArgs e, string columnDescription, IsUnicodeColumn myIsUnicodeColumn)
        {
            TextBox myTextBox = (TextBox)sender;
            myTextBox.Text = myTextBox.Text.Trim();
            string sMessage = string.Empty;
            if (myIsUnicodeColumn == IsUnicodeColumn.No)
            {
                if (MyFunction.GetStringByteLength(myTextBox.Text) > myTextBox.MaxLength == true)
                {
                    sMessage = columnDescription + "的长度超过" + myTextBox.MaxLength.ToString()+"\r\n汉字算2位";
                    MyTip.WarnTip(sMessage);
                    e.Cancel = true;
                    //if (MyTip.AskTip("长度超出\r\n" + columnDescription + "的长度超过" + myTextBox.MaxLength.ToString() + "位\r\n是否继续验证?") == DialogResult.No)
                    //{
                    //    e.Cancel = false;
                    //}
                    //else
                    //{
                    //    e.Cancel = true;
                    //}
                }
            }
            else
            {
                if (MyFunction.GetStringUnicodeLength(myTextBox.Text) > myTextBox.MaxLength == true)
                {
                    sMessage = columnDescription + "的长度超过" + myTextBox.MaxLength.ToString() ;
                    MyTip.WarnTip(sMessage);
                    e.Cancel = true;
                }
            }
        }

        static public void MyTextBoxIntegerValidating(object sender, CancelEventArgs e, string columnDescription)
        {
            TextBox myTextBox = (TextBox)sender;
            myTextBox.Text = myTextBox.Text.Trim();
            if (CheckValid.IsValidInteger(myTextBox.Text) == false)
            {
                MyTip.WarnTip( columnDescription + "不是整数");
                //if (MyTip.AskTip("数据不合法\r\n" + columnDescription + "不是整数\r\n是否继续验证?") == DialogResult.No)
                //{
                //    e.Cancel = false;
                //}
                //else
                //{
                e.Cancel = true;
                //}
            }
        }
              static public void MyTextBoxFloatValidating(object sender, CancelEventArgs e, string columnDescription)
        {
            TextBox myTextBox = (TextBox)sender;
            myTextBox.Text = myTextBox.Text.Trim();
            if (CheckValid.IsValidFloat(myTextBox.Text) == false)
            {
                MyTip.WarnTip(columnDescription + "不是小数");
                //if (MyTip.AskTip("数据不合法\r\n" + columnDescription + "不是整数\r\n是否继续验证?") == DialogResult.No)
                //{
                //    e.Cancel = false;
                //}
                //else
                //{
                e.Cancel = true;
                //}
            }
        }
        static public void MyComboBoxIntegerValidating(object sender, CancelEventArgs e, string columnDescription)
        {
            ComboBox myComboBox = (ComboBox)sender;
            myComboBox.Text = myComboBox.Text.Trim();
            if (CheckValid.IsValidInteger(myComboBox.Text) == false)
            {
                MyTip.WarnTip(columnDescription + "不是整数");
                //if (MyTip.AskTip("数据不合法\r\n" + columnDescription + "不是整数\r\n是否继续验证?") == DialogResult.No)
                //{
                //    e.Cancel = false;
                //}
                //else
                //{
                e.Cancel = true;
                //}
            }
        }
        static public void MyComBoxValidating(object sender, CancelEventArgs e, string columnDescription, IsUnicodeColumn myIsUnicodeColumn)
        {
            ComboBox myTextBox = (ComboBox)sender;
            myTextBox.Text = myTextBox.Text.Trim();
            string sMessage = string.Empty;
            if (myIsUnicodeColumn == IsUnicodeColumn.No)
            {
                if (MyFunction.GetStringByteLength(myTextBox.Text) > myTextBox.MaxLength == true)
                {
                    sMessage = columnDescription + "的长度超过" + myTextBox.MaxLength.ToString() + "\r\n汉字算2位";
                    MyTip.WarnTip(sMessage);
                    e.Cancel = true;
                  
                }
            }
            else
            {
                if (MyFunction.GetStringUnicodeLength(myTextBox.Text) > myTextBox.MaxLength == true)
                {
                    sMessage = columnDescription + "的长度超过" + myTextBox.MaxLength.ToString();
                    MyTip.WarnTip(sMessage);
                    e.Cancel = true;
                }
            }
        }
    }
}
